package com.vichat.workflow.thread;

import com.alibaba.dubbo.container.spring.SpringContainer;
import com.vichat.workflow.service.IWorkflowServcie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vichat.workflow.entity.WfSchedule;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * Created by wangluyin on 2017/12/18.
 */

public class ProcessWfScheduleThread extends Thread {
	private static transient final Logger logger = LoggerFactory.getLogger(ProcessWfScheduleThread.class);

	private IWorkflowServcie workflowServcie;

	HashMap<String, String> tokenMap = new HashMap();

	public ProcessWfScheduleThread() {
		ApplicationContext ctx = SpringContainer.getContext();
		this.workflowServcie = (IWorkflowServcie) ctx.getBean("workflowServcie", IWorkflowServcie.class);
	}

	int corePoolSize = 10;
	int maximumPoolSize = 20;
	long keepAliveTime = 5;
	TimeUnit unit = TimeUnit.MINUTES;
	BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();

	public void run() {
		while (true) {
			try {
				List<WfSchedule> todoWfScheduleList = workflowServcie.getAutoTodoWfSchedule(10);
				logger.info("todoWfScheduleList:" + todoWfScheduleList.size());
				if (todoWfScheduleList != null && todoWfScheduleList.size() > 0) {
					for (WfSchedule wfSchedule: todoWfScheduleList) {
						workflowServcie.processWfSchedule(wfSchedule);
					}
				} else {
					Thread.sleep(5 * 1000);
				}

			} catch (Exception e) {
				e.printStackTrace();
				logger.error(e.getMessage(), e);
			}
		}
	}



}
